Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backoff

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backoff

Fibonacci and exponential backoffs.

  • 2.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
576K
decreased by-3.67%
Maintainers
1
Weekly downloads
 
Created

What is backoff?

The 'backoff' npm package provides a mechanism to handle retry logic with exponential backoff and other backoff strategies. It is useful for scenarios where you need to retry operations that may fail intermittently, such as network requests or database queries.

What are backoff's main functionalities?

Exponential Backoff

This feature allows you to implement an exponential backoff strategy using the Fibonacci sequence. The code sample demonstrates how to set up a Fibonacci backoff with a randomisation factor, initial delay, and maximum delay. It also shows how to handle the 'ready' and 'fail' events.

const backoff = require('backoff');

const fibonacciBackoff = backoff.fibonacci({ randomisationFactor: 0.2, initialDelay: 10, maxDelay: 1000 });

fibonacciBackoff.on('ready', function(number, delay) {
  console.log(`Retrying after ${delay} ms`);
  // Perform the operation that needs to be retried
});

fibonacciBackoff.on('fail', function() {
  console.log('Operation failed after maximum retries');
});

// Start the backoff process
fibonacciBackoff.backoff();

Retry with Custom Strategy

This feature allows you to implement a custom retry strategy using exponential backoff. The code sample demonstrates how to set up an exponential backoff with an initial delay and maximum delay. It also shows how to handle the 'ready' and 'fail' events.

const backoff = require('backoff');

const customStrategy = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });

customStrategy.on('ready', function(number, delay) {
  console.log(`Retry attempt #${number} after ${delay} ms`);
  // Perform the operation that needs to be retried
});

customStrategy.on('fail', function() {
  console.log('Operation failed after maximum retries');
});

// Start the backoff process
customStrategy.backoff();

Handling Errors

This feature demonstrates how to handle errors during the retry process. The code sample shows how to simulate an operation that may fail and how to retry the operation using exponential backoff until it succeeds or reaches the maximum number of retries.

const backoff = require('backoff');

const exponentialBackoff = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });

exponentialBackoff.on('ready', function(number, delay) {
  console.log(`Retry attempt #${number} after ${delay} ms`);
  // Simulate an operation that may fail
  const success = Math.random() > 0.5;
  if (!success) {
    exponentialBackoff.backoff();
  } else {
    console.log('Operation succeeded');
  }
});

exponentialBackoff.on('fail', function() {
  console.log('Operation failed after maximum retries');
});

// Start the backoff process
exponentialBackoff.backoff();

Other packages similar to backoff

Keywords

FAQs

Package last updated on 12 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc